package cn.bqjr.eily.samples.service.base.impl;

import cn.bqjr.eily.samples.constants.SysConstant;
import cn.bqjr.eily.samples.dao.base.MenuMapper;
import cn.bqjr.eily.samples.entity.po.Menu;
import cn.bqjr.eily.samples.entity.po.RoleMenu;
import cn.bqjr.eily.samples.service.base.IMenuService;
import cn.bqjr.eily.samples.service.base.IRoleMenuService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author shan.he@bqjr.cn
 * @since 2017-05-11
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {

    @Autowired
    private IRoleMenuService roleMenuService;

    @Override
    public List<Menu> selectRootMenuByRole(Long parentMenuId, List<Long> roleIdList, boolean includeBtn) {
        Wrapper<Menu> menuWrapper = new EntityWrapper<Menu>().eq(Menu.PARENTMENUID, parentMenuId)
                                                             .orderBy(Menu.SORTNO);
        if (!includeBtn) {
            menuWrapper.ne(Menu.TYPE, SysConstant.MenuType.BUTTON.getVal());
        }

        if (!CollectionUtils.isEmpty(roleIdList) && SysConstant.ADMIN_ROLE_ID == roleIdList.get(0).longValue()) {
            return selectList(menuWrapper);
        } else {
            List<Object> menuIdObjList = roleMenuService.selectObjs(
                    new EntityWrapper<RoleMenu>().setSqlSelect(RoleMenu.MENUID)
                                                 .in(RoleMenu.ROLEID, roleIdList));

            List<Long> menuIdList = new ArrayList<>(menuIdObjList.size());
            menuIdObjList.stream()
                         .filter(obj -> obj instanceof BigInteger)
                         .forEach(obj -> menuIdList.add(((BigInteger) obj).longValue()));
            return selectList(menuWrapper.in(Menu.MENUID, menuIdList));
        }
    }

    @Override
    public List<Menu> fillMenuTree(List<Menu> menuList, List<Long> roleIdList, boolean includeBtn) {
        menuList.stream()
                .filter(menu -> SysConstant.MenuType.CATALOG.getVal() == menu.getType().intValue()
                        && CollectionUtils.isEmpty(menu.getChild()))
                .forEach(menu -> menu.setChild(
                        fillMenuTree(
                                selectRootMenuByRole(menu.getMenuId(), roleIdList, includeBtn),
                                roleIdList,
                                includeBtn)));
        return menuList;
    }

    @Override
    public List<Long> selectMenuIdsByRole(Long roleId) {
        List<Object> objList = roleMenuService.selectObjs(
                new EntityWrapper<RoleMenu>().setSqlSelect(RoleMenu.MENUID)
                                             .eq(RoleMenu.ROLEID, roleId));
        List<Long> menuIdList = new ArrayList<>(objList.size());
        objList.stream()
               .filter(obj -> obj instanceof BigInteger)
               .forEach(obj -> menuIdList.add(((BigInteger) obj).longValue()));

        return menuIdList;
    }
}
